//
//  MyPointsListViewModel.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/11/7.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "MyPointsListViewModel.h"
#import "YunOrderListModel.h"
@interface MyPointsListViewModel ()
@property(nonatomic, assign) NSInteger pageNum;
@property(nonatomic, assign) NSInteger pageSize;
@property(nonatomic, strong) NSMutableArray<YunOrderListModel *> *dataSource;
@end

@implementation MyPointsListViewModel
- (void)transfrom {
    self.pageNum = 1;
    self.pageSize = 20;
    [self request];
}
- (NSArray<YunOrderListModel *> *)dataArray {
    return self.dataSource.copy;
}
- (void)refresh:(BOOL)header {
    if (header) {
        self.pageNum = 1;
        [self.dataSource removeAllObjects];
    } else {
        self.pageNum += 1;
    }
    [self request];
}
- (void)request {
    WK(weakSelf)
    NSMutableDictionary *requestMsg = [NSMutableDictionary dictionary];
    [requestMsg setValue:@(self.pageNum) forKey:@"pageNum"];
    [requestMsg setValue:@(self.pageSize) forKey:@"pageSize"];
    [requestMsg setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_MyPointsList];
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:requestMsg FinishBlock:^(id  _Nonnull data) {
        [SVProgressHUD dismiss];
        BaseResponse *response = [BaseResponse modelWithJSON:data];
        if (!response.success) {
            !weakSelf.refresh ?: weakSelf.refresh(YES);
            return;
        }
        NSArray *list = data[@"data"][@"list"];
        [weakSelf.dataSource addObjectsFromArray:[YunOrderListModel mj_objectArrayWithKeyValuesArray:list]];
        !weakSelf.refresh ?: weakSelf.refresh(self.dataSource.count != [data[@"data"][@"total"]integerValue]);
    } FailedBlock:^(NSError * _Nonnull error) {
        !weakSelf.refresh ?: weakSelf.refresh(YES);
        [SVProgressHUD dismiss];
    }];
}
#pragma mark - lazy
- (NSMutableArray<YunOrderListModel *> *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
@end
